package com.ssy.invoicing.invouser.controller;

import java.sql.SQLException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ssy.invoicing.common.controller.BaseController;
import com.ssy.invoicing.dao.support.BaseSqlDao.SplitPageResult;
import com.ssy.invoicing.invouser.bean.InvoUser;
import com.ssy.invoicing.invouser.service.InvoUserService;

@Controller
@RequestMapping("invoicing")
public class InvoUserController extends BaseController {
    Logger logger = LoggerFactory.getLogger(InvoUserController.class);
    
    @Resource(name="invoUserService")
    private InvoUserService invoUserService;

    /**
     * 查询用户信息
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("invoUserInfo")
    public ModelAndView invoUserInfo(HttpServletRequest request, ModelMap model) {
        InvoUser sessionUser = (InvoUser) request.getSession().getAttribute("user");
        if (sessionUser == null) {
            model.put("message", "请重新登录");
            return new ModelAndView("login/index");
        }
        InvoUser invoUser = invoUserService.getInvoUserById(sessionUser.getId());
        model.put("userinfo", invoUser);
        return new ModelAndView("pages/invouser/userinfo");
    }
    
    /**
     * 查询用户信息列表
     * @param model
     * @return
     */
    @RequestMapping("listUser")
    public ModelAndView listUser(Integer page, ModelMap model) {
        try {
            SplitPageResult spr = invoUserService.queryUserListByPage(page);
            model.put("pagination", spr.getPagination());
            model.put("userList", spr == null ? null : spr.getResult());
        } catch (SQLException e) {
            logger.error("query user list fail.",e);
            return new ModelAndView("pages/error/err");
        }
        return new ModelAndView("pages/invouser/list");
    }
    
    /**
     * @return
     */
    @RequestMapping("addUser")
    public ModelAndView addUser() {
        return new ModelAndView("pages/invouser/add");
    }
    
    /**
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("saveUser")
    public ModelAndView saveUser(InvoUser user, ModelMap model) {
        invoUserService.saveUser(user);
        return new ModelAndView("redirect:listUser.htm");
    }
    
    /**
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("editUser")
    public ModelAndView editUser(Integer id, ModelMap model) {
        InvoUser invoUser = invoUserService.getInvoUserById(id);
        model.put("invoUser", invoUser);
        return new ModelAndView("pages/invouser/edit");
    }
    
    /**
     * @param user
     * @param model
     * @return
     */
    @RequestMapping("updateUser")
    public ModelAndView updateUser(InvoUser user, ModelMap model) {
        invoUserService.updateUser(user);
        return new ModelAndView("redirect:listUser.htm");
    }
    
    /**
     * @param id
     * @return
     */
    @RequestMapping("removeUser")
    public ModelAndView removeUser(Integer id) {
        invoUserService.removeUser(id);
        return new ModelAndView("redirect:listUser.htm");
    }
    
}
